﻿#region Copyright (c) 2010 R. Uittenbosch
/*
* Copyright (C) 2010 R. Uittenbosch
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
* associated documentation files (the "Software"), to deal in the Software without restriction, including 
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
* following conditions:
* 
* The above copyright notice and this permission notice shall be included in all copies or substantial 
* portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
* EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#endregion

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace TouchUI.System.Mobile.Mvc
{
  /// <summary>
  /// Custom textbox with rounded edges
  /// </summary>
  public class ViewTextBox : UserControl
  {

    public event EventHandler EnterPressed;
    // The requires bitmap and graphics objects
    private Bitmap _bmpBackground;
    private Graphics _grBackground;

    // The textbox so we can still enter text.
    private TextBox m_TextBox;
    private SizeF m_ScaleFactor = new SizeF(1f, 1f);

    /// <summary>
    /// The Text the user typed
    /// </summary>
    public override string Text
    {
      get
      {
        // Delegate to the real textbox
        return m_TextBox.Text;
      }
      set
      {
        // Delegate to the real textbox
        m_TextBox.Text = value;
        m_TextBox.ForeColor = Color.Gray;
      }
    }

    public Color TextBackColor { set { m_TextBox.BackColor = value; } get { return m_TextBox.BackColor; } }
    public Color TextForeColor { set { m_TextBox.ForeColor = value; } get { return m_TextBox.ForeColor; } }

    /// <summary>
    /// Constructor
    /// </summary>
    public ViewTextBox()
    {
      
      // Create the textbox
      this.m_TextBox = new TextBox()
      {
        BackColor   = Color.White,
        Multiline   = true,
        BorderStyle = BorderStyle.None,
        Font        = Common.Instance.SmallFont,
        ForeColor   = Color.Black
      };
      m_TextBox.GotFocus += new EventHandler(m_TextBox_GotFocus);
      m_TextBox.LostFocus += new EventHandler(m_TextBox_LostFocus);
      m_TextBox.KeyUp += new KeyEventHandler(m_TextBox_KeyUp);
      Controls.Add(this.m_TextBox);
      this.BackColor = Color.Transparent;
      // Position the textbox so it's in the middel
      this.PositionTextBox();
    }

    void m_TextBox_KeyUp(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Enter)
        if (EnterPressed != null)
          EnterPressed(this, EventArgs.Empty);
    }

    void m_TextBox_LostFocus(object sender, EventArgs e)
    {
        OnLostFocus(EventArgs.Empty);
    }

    void m_TextBox_GotFocus(object sender, EventArgs e)
    {
        OnGotFocus(EventArgs.Empty);
    }

    /// <summary>
    /// Position the textbox, depending on the viewtextbox bounds
    /// </summary>
    private void PositionTextBox()
    {
      int x       = (int)Math.Round((double)((0.1f * base.Width) + (0.12f * base.Height)));
      int y       = (int)Math.Round((double)(0.3f * base.Height));
      int width   = (int)Math.Round((double)((0.8f * base.Width) - (0.24f * base.Height)));
      int height  = (int)Math.Round((double)(0.4f * base.Height));
      this.m_TextBox.Bounds = new Rectangle(x, y, width, height);
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
      // Nothing
    }
    #region ICustomPainting Members
    
    /// <summary>
    /// The TouchUI kind of painting
    /// </summary>
    /// <param name="g"></param>
    public new void Paint(Graphics g)
    {
      //Delegate to the Windows kind
      base.OnPaint(new PaintEventArgs(g,new Rectangle()));
    }

    #endregion

    private void DrawImage()
    {
      if (_bmpBackground != null)
        _bmpBackground.Dispose();
      if (_grBackground != null)
        _grBackground.Dispose();

      _bmpBackground = new Bitmap(base.Width, base.Height);
      _grBackground = Graphics.FromImage(_bmpBackground);
      _grBackground.Clear(this.BackColor);
      // Where should we put the box
      float x = base.Width * 0.025f;
      float y = base.Height * 0.15f;
      float width = 0.95f * (base.Width - 1);
      float height = 0.7f * (base.Height - 1);
      // Draw it transparent so the actual textbox will show.
      _grBackground.DrawRoundedRectangle(new Pen(Color.Transparent, 1f),
        TextBackColor,
        new Rectangle((int)x, (int)y, (int)width, (int)height),
        new Size(20, 20));       
    }
    /// <summary>
    /// The Windows kind of painting
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPaint(PaintEventArgs e)
    {
      // Draw on the background to avoide flickering,
      // Draw only once and otherwhise just so the picture.
      if (_bmpBackground == null)
        DrawImage();

      e.Graphics.DrawImage(_bmpBackground, 0, 0);
      // And draw the underlying textbox
      base.OnPaint(e); 
    }

   
    protected override void OnResize(EventArgs e)
    {
      base.OnResize(e);
      if (this.m_TextBox != null)
      {
        this.PositionTextBox();
        DrawImage();
      }
    }

  
  }
}
